<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 单词记忆</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: "Microsoft YaHei", sans-serif;
            /* 渐变背景 + 抽象几何图案 */
            background: linear-gradient(135deg, #e8f4f8 0%, #f0f8fb 100%);
            background-image:
                    radial-gradient(circle at 20% 30%, rgba(64, 158, 255, 0.08) 0%, transparent 25%),
                    radial-gradient(circle at 80% 70%, rgba(64, 158, 255, 0.08) 0%, transparent 25%),
                    radial-gradient(circle at 40% 80%, rgba(103, 193, 249, 0.06) 0%, transparent 30%),
                    radial-gradient(circle at 70% 20%, rgba(103, 193, 249, 0.06) 0%, transparent 30%);
            background-attachment: fixed;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
            overflow: hidden;
        }
        /* 装饰元素 - 圆形光斑 */
        body::before, body::after {
            content: '';
            position: fixed;
            border-radius: 50%;
            background: rgba(64, 158, 255, 0.05);
            filter: blur(50px);
            z-index: -1;
        }
        body::before {
            width: 400px;
            height: 400px;
            top: -100px;
            left: -100px;
        }
        body::after {
            width: 500px;
            height: 500px;
            bottom: -150px;
            right: -150px;
        }
        .login-container {
            background-color: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            padding: 40px 30px;
            border-radius: 16px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.06);
            width: 100%;
            max-width: 380px;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            border: 1px solid rgba(255, 255, 255, 0.8);
        }
        .login-container:hover {
            transform: translateY(-5px);
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.08);
        }
        h1 {
            text-align: center;
            margin-bottom: 30px;
            color: #2c3e50;
            font-size: 26px;
            position: relative;
            padding-bottom: 15px;
            letter-spacing: 0.5px;
        }
        h1::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 50px;
            height: 3px;
            background: linear-gradient(90deg, #409eff, #67c2f9);
            border-radius: 3px;
        }
        .form-group {
            margin-bottom: 20px;
            position: relative;
        }
        .form-group .input-icon {
            position: absolute;
            left: 12px;
            top: 38px;
            color: #909399;
            font-size: 18px;
            pointer-events: none;
            transition: color 0.3s ease;
        }
        .form-group:focus-within .input-icon {
            color: #409eff;
        }
        label {
            display: block;
            margin-bottom: 8px;
            color: #666;
            font-size: 14px;
            font-weight: 500;
        }
        input {
            width: 100%;
            padding: 12px 15px 12px 40px;
            border: 1px solid #e0e6ed;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
            background-color: rgba(255, 255, 255, 0.8);
        }
        input:focus {
            border-color: #409eff;
            outline: none;
            box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.15);
            background-color: #fff;
        }
        input::placeholder {
            color: #c0c4cc;
        }
        button {
            width: 100%;
            padding: 12px;
            background: linear-gradient(90deg, #409eff, #67c2f9);
            color: #fff;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            margin-top: 15px;
            transition: background 0.3s ease, transform 0.1s ease, box-shadow 0.3s ease;
            box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
            position: relative;
            overflow: hidden;
        }
        button:hover:not(:disabled) {
            background: linear-gradient(90deg, #3088e0, #409eff);
            box-shadow: 0 6px 16px rgba(64, 158, 255, 0.25);
        }
        button:active:not(:disabled) {
            transform: scale(0.98);
            box-shadow: 0 2px 8px rgba(64, 158, 255, 0.2);
        }
        button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }
        .loading-spinner {
            display: inline-block;
            width: 16px;
            height: 16px;
            border: 2px solid rgba(255, 255, 255, 0.3);
            border-top-color: #fff;
            border-radius: 50%;
            animation: spin 0.6s linear infinite;
            margin-right: 8px;
            vertical-align: middle;
        }
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        .message {
            text-align: center;
            margin-top: 18px;
            padding: 10px 12px;
            font-size: 14px;
            min-height: 24px;
            border-radius: 6px;
            transition: all 0.3s ease;
            opacity: 0;
            transform: translateY(-5px);
        }
        .message.show {
            opacity: 1;
            transform: translateY(0);
        }
        .message.error {
            color: #f56c6c;
            background-color: #fef0f0;
            border: 1px solid #fde2e2;
        }
        .message.success {
            color: #67c23a;
            background-color: #f0f9ff;
            border: 1px solid #c6f6d5;
        }
        .extra-links {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
            font-size: 14px;
        }
        .extra-links a {
            color: #409eff;
            text-decoration: none;
            transition: color 0.2s ease, text-shadow 0.2s ease;
        }
        .extra-links a:hover {
            color: #3088e0;
            text-decoration: underline;
            text-shadow: 0 0 2px rgba(64, 158, 255, 0.1);
        }
    </style>
</head>
<body>
<div class="login-container">
    <h1>单词记忆</h1>
    <form id="loginForm">
        <div class="form-group">
            <label for="username">用户名</label>
            <span class="input-icon">👤</span>
            <input type="text" id="username" name="username" placeholder="请输入用户名" autocomplete="username" required>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <span class="input-icon">🔒</span>
            <input type="password" id="password" name="password" placeholder="请输入密码" autocomplete="current-password" required>
        </div>
        <button type="submit">登录</button>
        <div class="message" id="message"></div>
        <div class="extra-links">
            <a href="#">忘记密码?</a>
            <a href="/register.html">注册账号</a>
        </div>
    </form>
</div>

<script>
    const loginForm = document.getElementById('loginForm');
    const submitButton = loginForm.querySelector('button[type="submit"]');
    const messageEl = document.getElementById('message');
    const usernameInput = document.getElementById('username');
    const passwordInput = document.getElementById('password');

    // 显示消息函数
    function showMessage(text, type = 'error') {
        messageEl.textContent = text;
        messageEl.className = `message ${type} show`;
        
        // 3秒后自动隐藏
        setTimeout(() => {
            messageEl.classList.remove('show');
        }, 3000);
    }

    // 设置加载状态
    function setLoading(isLoading) {
        if (isLoading) {
            submitButton.disabled = true;
            submitButton.innerHTML = '<span class="loading-spinner"></span>登录中...';
        } else {
            submitButton.disabled = false;
            submitButton.textContent = '登录';
        }
    }

    // 清除消息
    usernameInput.addEventListener('input', () => messageEl.classList.remove('show'));
    passwordInput.addEventListener('input', () => messageEl.classList.remove('show'));

    // 登录表单提交
    loginForm.addEventListener('submit', async (e) => {
        e.preventDefault();
        
        const username = usernameInput.value.trim();
        const password = passwordInput.value;

        // 前端验证
        if (!username) {
            showMessage('请输入用户名', 'error');
            usernameInput.focus();
            return;
        }
        if (!password) {
            showMessage('请输入密码', 'error');
            passwordInput.focus();
            return;
        }
        if (password.length < 6) {
            showMessage('密码长度至少为6位', 'error');
            passwordInput.focus();
            return;
        }

        setLoading(true);

        try {
            const response = await fetch('/api/login', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ username, password }),
                credentials: 'include'
            });

            const result = await response.json();
            
            if (result.success) {
                showMessage('登录成功，正在跳转...', 'success');
                // 延迟跳转，让用户看到成功提示
                setTimeout(() => {
                    window.location.href = '/home.html';
                }, 800);
            } else {
                setLoading(false);
                showMessage(result.message || '登录失败，请重试', 'error');
            }
        } catch (err) {
            console.error('登录请求失败：', err);
            setLoading(false);
            showMessage('网络错误，请检查网络连接后重试', 'error');
        }
    });

    // 页面加载时聚焦用户名输入框
    window.addEventListener('load', () => {
        usernameInput.focus();
    });
</script>
</body>
</html>